<?php
/**
 * @name /www/unit-tests/User.php
 * @author
 * @version 2011.04.2
 *
 * handling changes to user information
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/User.php');

$UserId = $_POST['UserId'];
$UserName = $_POST['UserName'];
$Password = $_POST['Password'];
$ReTypePassword = $_POST['ReTypePassword'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$PhoneNumber = $_POST['PhoneNumber'];
$Email = $_POST['Email'];
$Street1 = $_POST['Street1'];
$Street2 = $_POST['Street2'];
$Street3 = $_POST['Street3'];
$City = $_POST['City'];
$State = $_POST['State'];
$Country = $_POST['Country'];
$PostalCode = $_POST['PostalCode'];

if(isset($_POST['NewsletterSubscription']) && $_POST['NewsletterSubscription'] == 'yes')
	$NewsletterSubscription = 1;
else
	$NewsletterSubscription = 0;

$Notes = $_POST['Notes'];

$Roles = $_POST['Roles'];

$UserRoles = explode("|", $Roles);

$Action = $_POST['Action'];


if($Action=="Update User Info")
{
	$u = new User();

	$u->Load($UserId);

	$u->UserName = $UserName;
	$u->FirstName = $FirstName;
	$u->LastName = $LastName;
	$u->PhoneNumber = $PhoneNumber;
	$u->Email = $Email;
	$u->Street1 = $Street1;
	$u->Street2 = $Street2;
	$u->Street3 = $Street3;
	$u->City = $City;
	$u->State = $State;
	$u->Country = $Country;
	$u->PostalCode = $PostalCode;
	$u->NewsletterSubscription = $NewsletterSubscription;
	$u->Notes = $Notes;

	$u->RevokeAllRoles();

	foreach($UserRoles as $ur)
		$u->GrantRole($ur);

	$u->save();

?>
	<script language="javascript">
		if(window.confirm("User information has been added successfully.\nDo you want to update more user information?"))
			document.location.replace("users.php");
		else
			document.location.replace("account.php");
	</script>
<?
}

else if($Action=="Update My Info")
{
	$u = new User();

	$u->Load($UserId);

	$u->FirstName = $FirstName;
	$u->LastName = $LastName;
	$u->PhoneNumber = $PhoneNumber;
	$u->Email = $Email;
	$u->Street1 = $Street1;
	$u->Street2 = $Street2;
	$u->Street3 = $Street3;
	$u->City = $City;
	$u->State = $State;
	$u->Country = $Country;
	$u->PostalCode = $PostalCode;
	$u->NewsletterSubscription = $NewsletterSubscription;

	$u->RevokeAllRoles();

	foreach($UserRoles as $ur)
		$u->GrantRole($ur);

	$u->save();
?>
	<script language="javascript">
		alert("Your information has been updated successfully");
		document.location.replace("account.php");
	</script>
<?
}

else if($Action=="Update Login Info")
{
	$u = new User();

	$u->Load($UserId);

	if($UserName!='')
		$u->UserName = $UserName;

	if($Password!='' && $ReTypePassword!='')
		$u->_PasswordHash = sha1($Password);

	$u->save();
?>
	<script language="javascript">
		alert("Your login information has been updated successfully");
		document.location.replace("account.php");
	</script>

<?
}
else if($Action=="Delete User")
{
	$u = new User();

	$u->Delete($UserId);
?>
	<script language="javascript">
		if(window.confirm("This User has been deleted successfully.\nDo you want to delete more user?"))
			document.location.replace("users.php");
		else
			document.location.replace("account.php");
	</script>
<?
}

else if($Action=="Send User Name")
{
	$u = new User();

	if($u->Load($u->GetUserIdByEmail($Email))>0)
	{
		$message = "Your User Name is: ".$u->UserName;
		$subject = "Your User Name";
		$headers = 'From: webmaster@skm.org' . "\r\n" .
	    'Reply-To: webmaster@skm.com' . "\r\n" .
	    'X-Mailer: PHP/' . phpversion();

		mail($Email, $subject,$message,$headers);
?>
	<script language="javascript">
		alert("Your User Name is sent to your E-mail address.");
		document.location.replace("login_register.php");
	</script>
<?
	}
	else
	{
?>
		<script language="javascript">
			alert("Invalid E-mail Address!!");
			document.location.replace("login_register.php");
		</script>
<?
	}
}

else if($Action=="Send Password")
{
	$u = new User();

	if($u->Load($u->GetUserIdByEmail($Email))>0)
	{
		$length = 10;
    	$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    	$newPass ="";

    	for ($p = 0; $p < $length; $p++)
		{
        	$newPass .= $characters[mt_rand(0, strlen($characters))];
    	}

		$message = "Your New Password is: ".$newPass;
		print($message);

		$u->SetPassword($newPass);
		$u->Save();

		$subject = "Your New Password";
		$headers = 'From: webmaster@skm.org' . "\r\n" .
	    'Reply-To: webmaster@skm.com' . "\r\n" .
	    'X-Mailer: PHP/' . phpversion();

		mail($Email, $subject,$message,$headers);
?>
	<script language="javascript">
		alert("Your New password is sent to your E-mail address.");
		document.location.replace("login_register.php");
	</script>
<?
	}
	else
	{
?>
		<script language="javascript">
			alert("Invalid E-mail Address!!");
			document.location.replace("login_register.php");
		</script>
<?
	}
}

	//php code for other actions will go here
?>

